Developer Documentation

QuickTime 4 API Documentation

Inside Macintosh: Memory

Previous | Chapter Top | Chapter Contents | Next |

Manipulating Blocks of Memory

The Memory Manager provides three routines for copying blocks of memory referenced by pointers. To copy a block of memory to a nonrelocatable block, use the BlockMove procedure. To copy to a new relocatable block, use the PtrToHand function. To copy to an existing relocatable block, use the PtrToXHand function. If you want to use any of these routines to copy memory you access with a handle, you must first dereference and lock the handle. A fourth routine, HandToHand , allows you to copy information from one handle to another.

To concatenate blocks of memory, you can use the HandAndHand and PtrAndHand functions.

BlockMove

To copy a sequence of bytes from one location in memory to another, you can use the BlockMove procedure.

PROCEDURE BlockMove (sourcePtr, destPtr: Ptr; byteCount: Size);
sourcePtr
The address of the first byte to copy.
destPtr
The address of the first byte to copy to.
byteCount
The number of bytes to copy. If the value of byteCount is 0, BlockMove does nothing.

DESCRIPTION

The BlockMove procedure moves a block of byteCount consecutive bytes from the address designated by sourcePtr to that designated by destPtr . It updates no pointers.

The BlockMove procedure works correctly even if the source and destination blocks overlap.

SPECIAL CONSIDERATIONS

You can safely call BlockMove at interrupt time. Even though it moves memory, BlockMove does not move relocatable blocks, but simply copies bytes.

The BlockMove procedure currently flushes the processor caches whenever the number of bytes to be moved is greater than 12. This behavior can adversely affect your application's performance. You might want to avoid calling BlockMove to move small amounts of data in memory if there is no possibility of moving stale data or instructions. For more information about stale data and instructions, see the discussion of the processor caches in the chapter "Memory Management Utilities" in this book.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for BlockMove are

Registers on entry

A0

Pointer to source

A1

Pointer to destination

D0

Number of bytes to copy

Registers on exit

D0

Result code

RESULT CODE

noErr

0

No error

PtrToHand

To copy data referenced by a pointer to a new relocatable block, use the PtrToHand function.

FUNCTION PtrToHand (srcPtr: Ptr; VAR dstHndl: Handle;
                                         size: LongInt): OSErr;
srcPtr
The address of the first byte to copy.
dstHndl
A handle for which you have not yet allocated any memory. The PtrToHand function allocates memory for the handle and copies size bytes beginning at srcPtr into it.
size
The number of bytes to copy.

DESCRIPTION

The PtrToHand function returns, in dstHndl , a newly created handle to a copy of the number of bytes specified by the size parameter, beginning at the location specified by srcPtr . The dstHndl parameter must be a handle variable that is not empty and is not a handle to an allocated block of size 0.

SPECIAL CONSIDERATIONS

Because PtrToHand allocates memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for PtrToHand are

Registers on entry

A0

Pointer to source

D0

Number of bytes to copy

Registers on exit

A0

Destination handle

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

SEE ALSO

You can use the PtrToHand function to copy data from one handle to a new handle if you dereference and lock the source handle. However, if you want to copy all of the data from one handle to another, the HandToHand function (described on HandToHand ) is more efficient.

PtrToXHand

To copy data referenced by a pointer to an already existing relocatable block, use the PtrToXHand function.

FUNCTION PtrToXHand (srcPtr: Ptr; dstHndl: Handle; size: LongInt):
                                         OSErr;
srcPtr
The address of the first byte to copy.
dstHndl
A handle to an already existing relocatable block to which to copy size bytes, beginning at srcPtr .
size
The number of bytes to copy.

DESCRIPTION

The PtrToXHand function makes the existing handle, specified by dstHndl , a handle to a copy of the number of bytes specified by the size parameter, beginning at the location specified by srcPtr .

SPECIAL CONSIDERATIONS

Because PtrToXHand affects memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for PtrToXHand are

Registers on entry

A0

Pointer to source

A1

Handle to destination

D0

Number of bytes to copy

Registers on exit

A0

Handle to destination

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block

HandToHand

Use the HandToHand function to copy all of the data from one relocatable block to a new relocatable block.

FUNCTION HandToHand (VAR theHndl: Handle): OSErr;
theHndl
On entry, a handle to the relocatable block whose data is to be copied. On exit, a handle to a new relocatable block whose data duplicates that of the original.

DESCRIPTION

The HandToHand function attempts to copy the information in the relocatable block to which theHndl is a handle; if successful, HandToHand returns a handle to the new relocatable block in theHndl . The new relocatable block is created in the same heap zone as the original block (which might not be the current heap zone).

Because HandToHand replaces its input parameter with the new handle, you should retain the original value of the input parameter somewhere else, or you won't be able to access it. Here is an example:

VAR
    original, copy: Handle;
    myErr: OSErr;
...
    copy := original;                   {both handles access same block}
    myErr := HandToHand(copy);          {copy now points to copy of block}

SPECIAL CONSIDERATIONS

If successful in creating a new relocatable block, the HandToHand function does not duplicate the properties of the original block. The new block is unlocked, unpurgeable, and not a resource. You might need to call HLock , HPurge , or HSetRBit (or the combination of HGetState and HSetState ) to adjust the properties of the new block.

Because HandToHand allocates memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for HandToHand are

Registers on entry

A0

Handle to original data

Registers on exit

A0

Handle to copy of data

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block

SEE ALSO

If you want to copy only part of a relocatable block into a new relocatable block, use the PtrToHand function, described on PtrToHand , after locking and dereferencing a handle to the relocatable block to be copied.

HandAndHand

Use the HandAndHand function to concatenate two relocatable blocks.

FUNCTION HandAndHand (aHndl, bHndl: Handle): OSErr;
aHndl
A handle to the first relocatable block, whose contents do not change but are concatenated to the end of the second relocatable block.
bHndl
A handle to the second relocatable block, whose size the Memory Manager expands so that it can concatenate the information from aHndl to the end of the contents of this block.

DESCRIPTION

The HandAndHand function concatenates the information from the relocatable block to which aHndl is a handle onto the end of the relocatable block to which bHndl is a handle. The aHndl variable remains unchanged.

The HandAndHand function dereferences the handle aHndl . You must call the HLock procedure to lock the block before calling HandAndHand . Afterward, you can call the HUnlock procedure to unlock it. Alternatively, you can save the block's original state by calling the HGetState function, lock the block by calling HLock , and then restore the original settings by calling HSetState .

SPECIAL CONSIDERATIONS

Because HandAndHand moves memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for HandAndHand are

Registers on entry

A0

Handle to be concatenated

A1

Handle to contain itself, data from A0's handle

Registers on exit

A0

Handle to concatenated data

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block

PtrAndHand

Use the PtrAndHand function to concatenate part or all of a memory block to the end of a relocatable block.

FUNCTION PtrAndHand (pntr: Ptr; hndl: Handle; size: LongInt):
                                         OSErr;
pntr
A pointer to the beginning of the data that the Memory Manager is to concatenate onto the end of the relocatable block.
hndl
A handle to the relocatable block, whose size the Memory Manager expands so that it can concatenate the information from pntr onto the end of this block.
size
The number of bytes of the block referenced by pntr to be copied.

DESCRIPTION

The PtrAndHand function takes the number of bytes specified by the size parameter, beginning at the location specified by pntr , and concatenates them onto the end of the relocatable block to which hndl is a handle.

The contents of the source block remain unchanged.

SPECIAL CONSIDERATIONS

Because PtrAndHand allocates memory, you should not call it at interrupt time.

ASSEMBLY-LANGUAGE INFORMATION

The registers on entry and exit for PtrAndHand are

Registers on entry

A0

Pointer to data to copy

A1

Handle to relocatable block at whose end the copied data concatenated

A2

Number of bytes to concatenate

Registers on exit

A0

Handle to now-concatenated relocatable block

D0

Result code

RESULT CODES

noErr

0

No error

memFullErr

-108

Not enough memory

nilHandleErr

-109

NIL master pointer

memWZErr

-111

Attempt to operate on a free block


© 1997 Apple Computer, Inc.

Previous | Chapter Top | Chapter Contents | Next